package com.getpebble.android.framework.protocol.outbound;

import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.util.ByteUtils;
import com.getpebble.android.notifications.model.Actions.PblNotificationAction;
import com.getpebble.android.notifications.model.Actions.PblNotificationOpenOnPhoneAction;
import com.getpebble.android.notifications.model.Actions.PblNotificationReplyAction;
import com.getpebble.android.notifications.model.PblNotification;
import com.getpebble.android.notifications.model.PblNotification.NotificationContent;
import com.google.common.primitives.UnsignedInteger;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PblOutboundExtensibleNotificationMessage extends PblOutboundMessage {
    private static final String TAG = PblOutboundExtensibleNotificationMessage.class.getSimpleName();
    private OutboundAckNack mAckNack;
    private int mActionId;
    private List<Action> mActions;
    private final UnsignedInteger mAncsId = UnsignedInteger.valueOf(0);
    private List<Attribute> mAttributes;
    private NotificationCommand mCommand;
    private UnsignedInteger mFlags;
    private final Layout mLayoutId = Layout.DEFAULT;
    private OutboundNotificationMessageType mMessageType;
    private UnsignedInteger mNotificationId;
    private UnsignedInteger mPostTimeSecs;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$AttributeType = new int[AttributeType.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$Layout = new int[Layout.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$OutboundNotificationMessageType = new int[OutboundNotificationMessageType.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$Layout[Layout.DEFAULT.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$AttributeType[AttributeType.TITLE.ordinal()] = 1;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$AttributeType[AttributeType.BODY.ordinal()] = 2;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$OutboundNotificationMessageType[OutboundNotificationMessageType.ADD_NOTIFICATION.ordinal()] = 1;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$OutboundNotificationMessageType[OutboundNotificationMessageType.REMOVE_NOTIFICATION.ordinal()] = 2;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$OutboundNotificationMessageType[OutboundNotificationMessageType.ACTION_ACK_NACK.ordinal()] = 3;
            } catch (NoSuchFieldError e6) {
            }
        }
    }

    private class Action {
        private List<Attribute> mAttributes;
        private int mId;
        private ActionType mType;

        public Action(int id, ActionType type) {
            this.mId = id;
            this.mType = type;
            this.mAttributes = new LinkedList();
        }

        public Action(PblOutboundExtensibleNotificationMessage pblOutboundExtensibleNotificationMessage, PblNotificationAction action) {
            this(pblOutboundExtensibleNotificationMessage, action, ActionType.PEBBLE_PROTOCOL);
        }

        public Action(PblOutboundExtensibleNotificationMessage pblOutboundExtensibleNotificationMessage, PblNotificationAction action, ActionType actionType) {
            this(action.getId(), actionType);
            addAttribute(new Attribute(AttributeType.TITLE, action.getTitle().toString()));
            if (action instanceof PblNotificationReplyAction) {
                PblNotificationReplyAction replyAction = (PblNotificationReplyAction) action;
                if (!replyAction.getChoices().isEmpty()) {
                    addAttribute(new Attribute(AttributeType.CANNED_RESPONSES, ByteUtils.getUtf8StringList(replyAction.getChoices(), 512)));
                }
            }
        }

        private void addAttribute(Attribute attribute) {
            if (attribute.mContent.length > 0) {
                this.mAttributes.add(attribute);
            }
        }
    }

    public enum ActionType {
        ANCS(1),
        PEBBLE_PROTOCOL(2),
        TEXT_ACTION(3),
        PEBBLE_PROTOCOL_DISMISS(4),
        EMPTY_ACTION(5);
        
        private final int id;

        private ActionType(int id) {
            this.id = id;
        }

        public int getId() {
            return this.id;
        }
    }

    private static class Attribute {
        byte[] mContent;
        AttributeType mType;

        private static int getMaxStringLen(AttributeType type) {
            switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$AttributeType[type.ordinal()]) {
                case SMTPMessage.RETURN_FULL /*1*/:
                    return 64;
                case SMTPMessage.RETURN_HDRS /*2*/:
                    return 512;
                default:
                    return 128;
            }
        }

        private Attribute(AttributeType type, String content) {
            this(type, ByteUtils.getUtf8String(content, getMaxStringLen(type)));
        }

        private Attribute(AttributeType type, byte[] content) {
            this.mType = type;
            this.mContent = content;
        }

        public Attribute(AttributeType type, int value) {
            this(type, ByteUtils.unsignedInt2bytesLittleEndian(UnsignedInteger.valueOf((long) value)));
        }
    }

    public enum AttributeType {
        TITLE(1),
        SUBTITLE(2),
        BODY(3),
        TINY_ICON(4),
        SMALL_ICON(5),
        LARGE_ICON(6),
        ANCS_ACTION_ID(7),
        CANNED_RESPONSES(8);
        
        private final int id;

        private AttributeType(int id) {
            this.id = id;
        }

        public int getId() {
            return this.id;
        }
    }

    public enum IconId {
        CROSSMARK(1),
        CHECKMARK(2),
        SENT_MAIL(3),
        SENT_MESSAGE(5),
        PHONE_CHECKMARK(6);
        
        private final int id;

        private IconId(int id) {
            this.id = id;
        }

        public int getId() {
            return this.id;
        }
    }

    public enum Layout {
        DEFAULT(1);
        
        private final int id;

        private Layout(int id) {
            this.id = id;
        }

        public int getId() {
            return this.id;
        }
    }

    public enum NotificationCommand {
        ADD_NOTIFICATION(1);
        
        private final int id;

        private NotificationCommand(int id) {
            this.id = id;
        }

        public int getId() {
            return this.id;
        }
    }

    public enum OutboundAckNack {
        ACK(0),
        NACK(1);
        
        private final int id;

        private OutboundAckNack(int id) {
            this.id = id;
        }

        public int getId() {
            return this.id;
        }
    }

    public enum OutboundNotificationMessageType {
        ADD_NOTIFICATION(0),
        REMOVE_NOTIFICATION(1),
        ACTION_ACK_NACK(17);
        
        private final int id;

        private OutboundNotificationMessageType(int id) {
            this.id = id;
        }

        public int getId() {
            return this.id;
        }
    }

    private PblOutboundExtensibleNotificationMessage() {
        super(EndpointId.EXTENSIBLE_NOTIFICATION);
    }

    public static PblOutboundExtensibleNotificationMessage getAddNotificationMessage(PblNotification notification, UnsignedInteger notificationId) {
        PblOutboundExtensibleNotificationMessage message = new PblOutboundExtensibleNotificationMessage();
        message.mMessageType = OutboundNotificationMessageType.ADD_NOTIFICATION;
        message.mNotificationId = notificationId;
        message.mFlags = UnsignedInteger.valueOf(0);
        message.mCommand = NotificationCommand.ADD_NOTIFICATION;
        message.mPostTimeSecs = UnsignedInteger.valueOf(notification.getPostTimeLocalMillis() / 1000);
        message.getNotificationAttributes(notification);
        message.getNotificationActions(notification);
        return message;
    }

    public static PblOutboundExtensibleNotificationMessage getRemoveNotificationMessage(UnsignedInteger notificationId) {
        PblOutboundExtensibleNotificationMessage message = new PblOutboundExtensibleNotificationMessage();
        message.mMessageType = OutboundNotificationMessageType.REMOVE_NOTIFICATION;
        message.mNotificationId = notificationId;
        return message;
    }

    public static PblOutboundExtensibleNotificationMessage getAckNackActionMessage(UnsignedInteger notificationId, int actionId, OutboundAckNack ackNack) {
        PblOutboundExtensibleNotificationMessage message = new PblOutboundExtensibleNotificationMessage();
        message.mMessageType = OutboundNotificationMessageType.ACTION_ACK_NACK;
        message.mNotificationId = notificationId;
        message.mActionId = actionId;
        message.mAckNack = ackNack;
        message.mAttributes = new ArrayList();
        return message;
    }

    public void changeNotificationId(UnsignedInteger newId) {
        this.mCachedGetBytes = null;
        this.mMessageBody.clear();
        this.mNotificationId = newId;
    }

    public void addAttribute(AttributeType type, String content) {
        Attribute attribute = new Attribute(type, content);
        if (attribute.mContent.length > 0) {
            this.mAttributes.add(attribute);
        }
    }

    public void addAttribute(AttributeType type, int value) {
        this.mAttributes.add(new Attribute(type, value));
    }

    public UnsignedInteger getNotificationID() {
        return this.mNotificationId;
    }

    public synchronized byte[] getBytes() {
        if (!areBytesCached()) {
            addBytes(Byte.valueOf((byte) this.mMessageType.getId()));
            switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$OutboundNotificationMessageType[this.mMessageType.ordinal()]) {
                case SMTPMessage.RETURN_FULL /*1*/:
                    addBytes(Byte.valueOf((byte) this.mCommand.getId()));
                    addBytes(ByteUtils.unsignedInt2bytesLittleEndian(this.mFlags));
                    addBytes(ByteUtils.unsignedInt2bytesLittleEndian(this.mNotificationId));
                    addBytes(ByteUtils.unsignedInt2bytesLittleEndian(this.mAncsId));
                    addBytes(ByteUtils.unsignedInt2bytesLittleEndian(this.mPostTimeSecs));
                    addBytes(Byte.valueOf((byte) this.mLayoutId.getId()));
                    addBytes(Byte.valueOf((byte) this.mAttributes.size()));
                    addBytes(Byte.valueOf((byte) this.mActions.size()));
                    for (Attribute attribute : this.mAttributes) {
                        addBytes(Byte.valueOf((byte) attribute.mType.getId()));
                        addBytes(ByteUtils.int2uint16LittleEndian(attribute.mContent.length));
                        addBytes(attribute.mContent);
                    }
                    for (Action action : this.mActions) {
                        addBytes(Byte.valueOf((byte) action.mId));
                        addBytes(Byte.valueOf((byte) action.mType.getId()));
                        addBytes(Byte.valueOf((byte) action.mAttributes.size()));
                        for (Attribute attribute2 : action.mAttributes) {
                            addBytes(Byte.valueOf((byte) attribute2.mType.getId()));
                            addBytes(ByteUtils.int2uint16LittleEndian(attribute2.mContent.length));
                            addBytes(attribute2.mContent);
                        }
                    }
                    break;
                case SMTPMessage.RETURN_HDRS /*2*/:
                    addBytes(ByteUtils.unsignedInt2bytesLittleEndian(this.mNotificationId));
                    break;
                case ListInfo.INDETERMINATE /*3*/:
                    addBytes(ByteUtils.unsignedInt2bytesLittleEndian(this.mNotificationId));
                    addBytes(Byte.valueOf((byte) this.mActionId));
                    addBytes(Byte.valueOf((byte) this.mAckNack.getId()));
                    addBytes(Byte.valueOf((byte) this.mAttributes.size()));
                    for (Attribute attribute22 : this.mAttributes) {
                        addBytes(Byte.valueOf((byte) attribute22.mType.getId()));
                        addBytes(ByteUtils.int2uint16LittleEndian(attribute22.mContent.length));
                        addBytes(attribute22.mContent);
                    }
                    break;
            }
        }
        return super.getBytes();
    }

    private void getNotificationAttributes(PblNotification notification) {
        List<Attribute> attributes = new LinkedList();
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundExtensibleNotificationMessage$Layout[this.mLayoutId.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                NotificationContent content = notification.getNotificationContent();
                Attribute title = new Attribute(AttributeType.TITLE, content.notificationTitle);
                if (title.mContent.length > 0) {
                    attributes.add(title);
                }
                Attribute body = new Attribute(AttributeType.BODY, content.notificationBody);
                if (body.mContent.length > 0) {
                    attributes.add(body);
                    break;
                }
                break;
        }
        this.mAttributes = attributes;
    }

    private void getNotificationActions(PblNotification notification) {
        List<Action> actions = new LinkedList();
        boolean addedRealActions = false;
        PblNotificationOpenOnPhoneAction openAction = notification.findOpenOnPhoneAction();
        if (openAction != null) {
            actions.add(new Action(this, openAction));
            addedRealActions = true;
        } else {
            actions.add(new Action(-1, ActionType.EMPTY_ACTION));
        }
        PblNotificationAction dismissAction = notification.findDismissAction();
        if (dismissAction != null) {
            actions.add(new Action(this, dismissAction, ActionType.PEBBLE_PROTOCOL_DISMISS));
            addedRealActions = true;
        } else {
            actions.add(new Action(-2, ActionType.EMPTY_ACTION));
        }
        for (PblNotificationAction replyAction : notification.getReplyActions()) {
            actions.add(new Action(this, replyAction, ActionType.TEXT_ACTION));
            addedRealActions = true;
        }
        for (PblNotificationAction action : notification.getOtherActions()) {
            actions.add(new Action(this, action, ActionType.PEBBLE_PROTOCOL));
            addedRealActions = true;
        }
        if (!addedRealActions) {
            actions.clear();
        }
        this.mActions = actions;
    }
}
